package com.lanou.musicbox.fragment;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ListView;

import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.google.gson.Gson;
import com.lanou.musicbox.R;
import com.lanou.musicbox.adapter.SeekMusicAdapter;
import com.lanou.musicbox.base.BaseFragment;
import com.lanou.musicbox.bean.SeekMusic;
import com.lanou.musicbox.utils.VolleySingleton;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by dllo on 15/10/20.
 */
public class SeekMusicFragment extends BaseFragment implements View.OnClickListener, AdapterView.OnItemClickListener {
    private ImageView startSeek;
    private EditText seekeditText;
    private String seekXY;// 定义一个接收输入框内容
    private List<SeekMusic.DataEntity> data;
    private ListView seekmusicListView;
    private VolleySingleton volleySingleton;
    private SeekMusicAdapter adapter;
    private FragmentManager manager;// 管理fragment跳转

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.seekmusic, null);

        startSeek = (ImageView) view.findViewById(R.id.startSeek);
        seekeditText = (EditText) view.findViewById(R.id.seekeditText);
        seekmusicListView = (ListView) view.findViewById(R.id.seekmusicListView);
        volleySingleton = VolleySingleton.getVolleySingleton(getActivity());

        startSeek.setOnClickListener(this);
        seekmusicListView.setOnItemClickListener(this);

        initData();
        return view;

    }

    private void initData() {
        data = new ArrayList<>();
        String url = "http://api.dongting.com/misc/sug/billboard?uid=866058021842951&f=f3040&v=v8.2.0.2015091720&app=ttpod&utdid=" + seekXY + "&hid=1135212123029932&s=s200&alf=alf203200&imsi=460078098497828&tid=0&net=2&size=20";
        StringRequest request = new StringRequest(url, new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
                Gson gson = new Gson();
                SeekMusic s = gson.fromJson(response, SeekMusic.class);
                data = s.getData();
                adapter = new SeekMusicAdapter(data, getActivity());
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {

            }
        });
        volleySingleton.addQueue(request);
    }

    @Override
    public void onClick(View view) {
        seekXY = seekeditText.getText().toString();
        try {
            if (seekXY != null) {
                seekXY = URLEncoder.encode(seekXY, "utf-8");
            }
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }

        switch (view.getId()) {
            case R.id.startSeek:
                EnterTheSeekMusicFragment fragment = new EnterTheSeekMusicFragment();
//                Bundle bundle = new Bundle();
//                bundle.putString("song_id", adapter.getUrl(i));
//                fragment.setArguments(bundle);
                // 初始化transaction使其开始跳转
                FragmentTransaction transaction = manager.beginTransaction();
                transaction.addToBackStack(null);// 清理之前的fragment
//                transaction.replace(R.id.fill_mian, fragment);// 将要跳转到的fragment放到fill_mian的栈位中
                transaction.commit();
                break;
        }
    }

    @Override
    public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {

    }
}
